package Controller;

import Dao.PatternDao;
import Dao.StaffDao;
import Entity.Patient;
import Entity.Pattern;
import Entity.Question;
import Utils.SimpleTools;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.*;

public class AssessController extends Application implements Initializable {
    Stage stage = new Stage();
    private int i;
    private int score;
    private final Pattern pattern = PatternDao.StartAssess();
    List<Question> questionList = pattern.getQuestions();
    public AssessController() throws IOException {
        i = 0;
        score = 0;
    }

    /**————————————————————————测评————————————————————*/
    @FXML
    private AnchorPane FirstPane;
    @FXML
    private Label Title;
    @FXML
    private Label optiontext1;
    @FXML
    private CheckBox option1;
    @FXML
    private Label optiontext2;
    @FXML
    private CheckBox option2;
    @FXML
    private Label optiontext3;
    @FXML
    private CheckBox option3;
    @FXML
    private ImageView goButton;
    @FXML
    private ImageView backButton;
    @FXML
    private Label numberLabel;
    @FXML
    private AnchorPane SecondPane;
    @FXML
    void back(MouseEvent event) {
        i--;
        if(i==questionList.size()-1) goButton.setVisible(false);
        Question question = questionList.get(i);
        Title.setText(question.getContent());
        option1.setText(question.getOptions()[0]);
        option2.setText(question.getOptions()[1]);
        option3.setText(question.getOptions()[2]);
        if(option3.isSelected()==question.getAnswer()[2]) score--;
        if(option2.isSelected()==question.getAnswer()[1]) score--;
        if(option1.isSelected()==question.getAnswer()[0]) score--;
        numberLabel.setText(i+1+"/"+questionList.size());
        option3.setSelected(false);
        option2.setSelected(false);
        option1.setSelected(false);
    }

    @FXML
    void go(MouseEvent event) {
        i++;
        if(i==1) backButton.setVisible(true);
        if(i==questionList.size()-1) goButton.setVisible(false);
        Question question = questionList.get(i);
        Title.setText(question.getContent());
        option1.setText(question.getOptions()[0]);
        option2.setText(question.getOptions()[1]);
        option3.setText(question.getOptions()[2]);
        if(option3.isSelected()==question.getAnswer()[2]) score++;
        if(option2.isSelected()==question.getAnswer()[1]) score++;
        if(option1.isSelected()==question.getAnswer()[0]) score++;
        numberLabel.setText(i+1+"/"+questionList.size());
        option3.setSelected(false);
        option2.setSelected(false);
        option1.setSelected(false);
    }

    /**————————————————————————评估————————————————————*/

    @FXML
    private TableView<Patient> userTable;
    @FXML
    private TableColumn<Patient, String> usernametable;
    @FXML
    private TableColumn<Patient, String> usersextable;
    @FXML
    private TableColumn<Patient, String> useragetable;
    @FXML
    private TableColumn<Patient, String> userbedtable;
    @FXML
    private TableColumn<Patient, String> userassesstable;
    @FXML
    private Label ScoreLabel;
    @FXML
    private Label DateLabel;
    @FXML
    private TextArea AssessmentText;

    private final ObservableList<Patient> cellData = FXCollections.observableArrayList();
    @FXML
    void finishAssess(ActionEvent event) throws IOException {
        FirstPane.setVisible(false);
        SecondPane.setVisible(true);

        ScoreLabel.setText("总分："+3*questionList.size()+" 得分："+score);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm");
        String now = simpleDateFormat.format(new Date());
        DateLabel.setText(now);

        //初始化病患表

        usernametable.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getName()));
        usersextable.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getSex()));
        useragetable.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getAge()));
        userbedtable.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getRoom()));
        userassesstable.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getAssessment()));

        cellData.clear();
        cellData.addAll(StaffDao.getPatients());
        userTable.setItems(null);
        userTable.setItems(cellData);
    }

    @FXML
    void GetAssessment(MouseEvent event) {
        if(userTable.getSelectionModel().getSelectedItem()!=null){
            Patient patient = userTable.getSelectionModel().getSelectedItem();
            AssessmentText.setText(patient.getAssessment());
        }
    }

    @FXML
    void addAssessment(MouseEvent event) throws IOException {
        if(userTable.getSelectionModel().getSelectedItem()!=null){
            Patient patient = userTable.getSelectionModel().getSelectedItem();
            List<Patient> list = StaffDao.getPatients();
            for(Patient p:list){
                if(p.getPhone().equals(patient.getPhone())){
                    p.setAssessment(AssessmentText.getText());
                    break;
                }
            }
            StaffDao.StorePatients(list);
            SimpleTools.informationDialog(Alert.AlertType.INFORMATION, "提示", "", "保存成功");
            AssessmentText.clear();
        }
        else SimpleTools.informationDialog(Alert.AlertType.WARNING, "提示", "警告", "请选择您要评价的患者");

        cellData.clear();
        cellData.addAll(StaffDao.getPatients());
        userTable.setItems(null);
        userTable.setItems(cellData);
    }


    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        SimpleTools.clearFile();
        backButton.setVisible(false);
        goButton.setVisible(true);
        Title.setText(questionList.get(0).getContent());
        option1.setText(questionList.get(0).getOptions()[0]);
        option2.setText(questionList.get(0).getOptions()[1]);
        option3.setText(questionList.get(0).getOptions()[2]);
        numberLabel.setText(1+"/"+questionList.size());
    }
    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("../View/AssessController.fxml"));
        primaryStage.setTitle("评估");
        primaryStage.setResizable(false);
        primaryStage.setScene(new Scene(root, 600, 440));
        primaryStage.getIcons().add(new Image("file:src/resource/flour.png"));
        primaryStage.showAndWait();
    }
    public void  showWindow() throws Exception {
        start(stage);
    }
    public static void main(String[] args) {
        launch(args);
    }

}
